package cn.exrick.xboot.modules.gatestation.controller;

import cn.exrick.xboot.base.XbootBaseController;
import cn.exrick.xboot.common.utils.CustomPageUtil;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.utils.SnowFlakeUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.gatestation.entity.NaturalgasEquipment;
import cn.exrick.xboot.modules.gatestation.service.NaturalgasEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author yck
 */
@Slf4j
@RestController
@Api(description = "天燃气门站设备管理接口")
@RequestMapping("/xboot/naturalgasEquipment")
@Transactional
public class NaturalgasEquipmentController extends XbootBaseController<NaturalgasEquipment, String> {

    @Autowired
    private NaturalgasEquipmentService naturalgasEquipmentService;

    @Override
    public NaturalgasEquipmentService getService() {
        return naturalgasEquipmentService;
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Object> getByCondition(NaturalgasEquipment naturalgasEquipment,
                                                            SearchVo searchVo,
                                                            PageVo pageVo){

        Page<NaturalgasEquipment> page = naturalgasEquipmentService.findByCondition(naturalgasEquipment, searchVo, PageUtil.initPage(pageVo));

        return new ResultUtil<Object>().setData(CustomPageUtil.getCustomPage(page));
    }
    @RequestMapping(value = "/addEquipment", method = RequestMethod.POST)
    @ApiOperation(value = "批量插入设备")
    public Result<Object>  addEquipment(String[] nameArray,String[] idArray,String id){
        StringBuffer buffer=new  StringBuffer();
        for(int i=0;i<nameArray.length;i++){
            List<NaturalgasEquipment> byEquipmentNameAndAndGateStationId = naturalgasEquipmentService.findByEquipmentNameAndAndGateStationId(nameArray[i], id);
           if(byEquipmentNameAndAndGateStationId.size()>0){
               buffer.append(","+nameArray[i]);
           }
        }
        for(int i=0;i<idArray.length;i++){
            List<NaturalgasEquipment> byEquipmentIdAndGateStationId = naturalgasEquipmentService.findByEquipmentIdAndGateStationId(idArray[i], id);
            if(byEquipmentIdAndGateStationId.size()>0){
                buffer.append(","+idArray[i]);
            }
        }
        if(buffer.toString().length()>0){
            return   ResultUtil.error(buffer.toString().substring(1,buffer.toString().length())+"已添加！");
        }
        for(int i=0;i<nameArray.length;i++){
            NaturalgasEquipment  equipment=new NaturalgasEquipment();
            equipment.setId(SnowFlakeUtil.getFlowIdInstance().nextId()+"");
            equipment.setEquipmentId(idArray[i]);
            equipment.setEquipmentName(nameArray[i]);
            equipment.setGateStationId(id);
            naturalgasEquipmentService.save(equipment);
        }
        return ResultUtil.success("添加成功！");
    }
    //equipmenUpdate
    @RequestMapping(value = "/equipmenUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "设备修改")
    public  Result<Object> equipmenUpdate(String id,String equipmentName,String equipmentId,String gateStationId){
        NaturalgasEquipment naturalgasEquipment = naturalgasEquipmentService.get(id);
        List<NaturalgasEquipment> byContidion = naturalgasEquipmentService.findByContidion(equipmentId , gateStationId,equipmentName,id);
        if(byContidion.size()>0){
            return ResultUtil.error("设备名称或设备ID重复！");
        }else{
            naturalgasEquipment.setEquipmentName(equipmentName);
            naturalgasEquipment.setEquipmentId(equipmentId);
            naturalgasEquipmentService.update(naturalgasEquipment);
        }
        return ResultUtil.success("编辑成功！");
    }
}
